//proxy      代理商（连接了用户和真实的数据的中间层）
//reflect         （反射-object）
{
  let obj={
    time:'2017-03-11',
    name:'net',
    _r:123
  };

  //创建代理商proxy，映射obj对象，--》做了一些操作 ---》最后把结果传递给原始对象
  let monitor=new Proxy(obj,{
    // 拦截对象属性的读取
    get(target,key){
      return target[key].replace('2017','2018')
    },
    // 拦截对象设置属性
    set(target,key,value){
      if(key==='name'){
        return target[key]=value;
      }else{
        return target[key];
      }
    },
    // 拦截key in object操作
    has(target,key){
      if(key==='name'){
        return target[key]
      }else{
        return false;
      }
    },
    // 拦截delete
    deleteProperty(target,key){
      if(key.indexOf('_')>-1){   //如果要检索的字符串值没有出现，则该方法返回 -1
        delete target[key];
        return true;
      }else{
        return target[key]
      }
    },
    // 拦截Object.keys,Object.getOwnPropertySymbols,Object.getOwnPropertyNames
    ownKeys(target){
      return Object.keys(target).filter(item=>item!='time')   //过滤掉
    }
  });

  console.log('get',monitor.time);

  monitor.time='2018';
  monitor.name='mukewang';
  console.log('set',monitor.time,monitor);

  console.log('has','name' in monitor,'time' in monitor);

  // delete monitor.time;
  // console.log('delete',monitor);
  //
  // delete monitor._r;   //删除
  // console.log('delete',monitor);
  console.log('ownKeys',Object.keys(monitor));

}

{
  let obj={
    time:'2017-03-11',
    name:'net',
    _r:123
  };

  console.log('Reflect get',Reflect.get(obj,'time'));
  Reflect.set(obj,'name','mukewang');
  console.log(obj);
  console.log('has',Reflect.has(obj,'name'));
}


//解耦，和业务解耦的校验模块~！
//举例子：校验业务

{
    function validator(target,validator){    //返回proxy对象
        return new Proxy(target,{
            _validator:validator,
            set(target,key,value,proxy){
                if(target.hasOwnProperty(key)){
                    let va=this._validator[key];
                    if(!!va(value)){                              //
                        return Reflect.set(target,key,value,proxy)
                    }else{
                        throw Error(`不能设置${key}到${value}`)
                    }
                }else{
                    throw Error(`${key} 不存在`)                    //
                }
            }
        })
    }

    const personValidators={                                       //校验条件
        name(val){
            return typeof val==='string'
        },
        age(val){
            return typeof val === 'number' && val>18
        },
        mobile(val){

        }
    }

    class Person{
        constructor(name,age){
            this.name=name;
            this.age=age;
            this.mobile='1111';
            return validator(this,personValidators)
        }
    }

    const person=new Person('lilei',30);

    console.info(person);

    person.name='Han mei mei';

    console.info(person);
}
